package top.luo;

import top.luo.entity.User;
import top.luo.io.Resources;
import top.luo.mapper.UserMapper;
import top.luo.sqlSession.SqlSession;
import top.luo.sqlSession.SqlSessionFactory;
import top.luo.sqlSession.SqlSessionFactoryBuilder;

import java.util.List;

/**
 * @author luo
 * @date 2021-01-04 22:29
 */
public class MainClass {

    public static void main(String[] args) throws Exception {
//        test1();
//        test2();
        test3();
    }

    public static void test1() throws Exception {
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("/sqlMapConfig.xml"));
        SqlSession sqlSession = factory.openSession();

        User user = new User();
        user.setAge(12);
        List<User> users = sqlSession.selectList("top.luo.mapper.UserMapper.selectList", user);
        for (User u : users) {
            System.out.println(u);
        }
    }

    public static void test2() throws Exception {
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("/sqlMapConfig.xml"));
        SqlSession sqlSession = factory.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User userParam = new User();
        userParam.setId(1L);
        userParam.setUsername("Tom");
        User user = userMapper.selectOne(userParam);
        System.out.println(user);
    }

    public static void test3() throws Exception {
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("/sqlMapConfig.xml"));
        SqlSession sqlSession = factory.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User userParam = new User();
        userParam.setAge(12);
        List<User> users = userMapper.selectList(userParam);
        users.forEach(System.out::println);
    }


}
